home *** CD-ROM | disk | FTP | other *** search
/ The Business Master (3rd Edition) / The Business Master (3rd Edition).iso / files / business / stata3 / graphics.tut < prev    next >
Encoding:
Text File  |  1988-08-30  |  27.4 KB  |  804 lines

  1. set output error
  2. set display page 23
  3. set more 1
  4. #delimit ;
  5.  
  6. di _n(5) in wh
  7. "  ___  ____  ____  ____  ____ tm" _n
  8. " /__    /   ____/   /   ____/" _n
  9. "___/   /   /___/   /   /___/    How to Make Graphs" _n
  10. "--------------------------------------------------" _n(2) ;
  11.  
  12. di in gr
  13. "We are about to demonstrate Stata's graphics.  Actually, we will only scratch"
  14. _n
  15. "the surface of those capabilities, so if you own the professional version of"
  16. _n
  17. "Stata, you should read the Graphics chapter of the Reference Manual.  If you"
  18. _n
  19. "have the Demonstration Version, after this tutorial you should feel free to" _n
  20. "experiment.  You can learn more about the "
  21. in wh "graph" in gr " command by typing "
  22. "'" in wh "help graph" in gr "'." _n ;
  23.  
  24.  
  25. di in gr
  26. "Some more words of advice:  If your computer and monitor do not have graphics"
  27. _n
  28. "capabilities, there is no reason to run this tutorial.  You can still use" _n
  29. "Stata's graphics, but you will have to print them in order to see them.  This"
  30. _n
  31. "tutorial assumes you will be able to see the graphs as we draw them.  You can"
  32. _n
  33. "abort this tutorial by pressing "
  34. in wh "Break" in gr " (DOS users hold down "
  35. in wh "Ctrl" in gr " and press" _n
  36. in wh "Break" in gr ", Unix users hold down "
  37. in wh "Ctrl" in gr " and press " in wh "C"
  38. in gr ").  On the next screen, we will" _n
  39. "verify that Stata thinks your computer has graphics capabilities." _n ;
  40.  
  41. di in gr
  42. "If you are using a Sun under SunView, please "
  43. in wh "Break" in gr" out of this tutorial, type"
  44. _n
  45. "'" in wh "window define tut"
  46. in gr "', and then restart this tutorial.  You don't have to do" _n
  47. "that, but results will be better if you do." _n;
  48.  
  49. #delimit cr
  50. mac def path
  51. capture run nullfile.tut
  52. if _rc {
  53.     mac def path "\stata\"
  54.     capture run %path`nullfile.tut
  55.     if _rc {
  56.         mac def path "/usr/stata/"
  57.         capture run %path`nullfile.tut
  58.         if _rc {
  59.             #delimit ;
  60.             di in red
  61. "I cannot find the other tutorial files.  I have looked in the current" _n
  62. "directory and in \stata (DOS) or /usr/stata (Unix).  Is Stata installed" _n
  63. "correctly?" _n(2)
  64. "In any case, I cannot run the tutorial." ;
  65.             #delimit cr
  66.             exit
  67.         }
  68.     }
  69. }
  70.  
  71. macro define F5 "do %path`contents.tut;"
  72. macro define F6 "do %path`tables.tut;"
  73.  
  74. if "%path"=="/usr/stata/" {
  75.     mac def _pause "more"
  76. }
  77.  
  78. #delimit ;
  79.  
  80.  
  81.  
  82. set more 0 ; more ; set more 1 ;
  83.  
  84. set textsize 100 ;
  85. di _n(4) in wh
  86. "More preliminary" _n
  87. "----------------" _n(2)
  88. ". query" ;
  89. noisily query ;
  90.  
  91. di in gr _n
  92. "If the above display indicates that "
  93. in wh "graphics" in gr " are " in wh "off"
  94. in gr ", Stata does not think" _n
  95. "your computer and monitor have graphics capabilities, so you may as well" _n
  96. in wh "Break" in gr " out of this tutorial." _n ;
  97.  
  98. set more 0 ; more ; set more 1;
  99. macro define F6 "do %path`graphics.tut;" ;
  100.  
  101.  
  102. di _n(9) in wh
  103. "Graphic styles" _n
  104. "--------------" _n ;
  105.  
  106. di in gr
  107. "Stata provides eight graphic styles, known as:" _n(2)
  108. _col(10) in wh "twoway" in gr _col(22) "meaning   two-way scatterplots" _n
  109. _col(10) in wh "matrix" in gr _col(22) "meaning   two-way scatterplot matrices"
  110. _n
  111. _col(10) in wh "histogram" in gr _col(22) "meaning   histograms" _n
  112. _col(10) in wh "oneway" in gr _col(22) "meaning   one-way scatterplots" _n
  113. _col(10) in wh "box" in gr _col(22) "meaning   box-and-whisker plots" _n
  114. _col(10) in wh "star" in gr _col(22) "meaning   star charts" _n
  115. _col(10) in wh "bar" in gr _col(22) "meaning   bar charts" _n
  116. _col(10) in wh "pie" in gr _col(22) "meaning   pie charts" _n ;
  117.  
  118. #delimit cr
  119.  
  120. capture program drop _yesno
  121. program define _yesno
  122.     macro define answ
  123.     while lower("%answ")!="y" & lower("%answ")!="n" {
  124.         di in gr "%_1? (" in wh "y" in gr "/" in wh "n" in gr ")" /*
  125.         */ _col(50) _request(answ)
  126.     }
  127. end
  128.  
  129.  
  130. _yesno "Would you like to see examples of each"
  131. if lower("%answ")=="y" {
  132.     set more 0
  133.     capture noisily graph using %path`twoway
  134.     %_pause
  135.     capture noisily graph using %path`matrix
  136.     %_pause
  137.     capture noisily graph using %path`hist
  138.     %_pause
  139.     capture noisily graph using %path`oneway
  140.     %_pause
  141.     capture noisily graph using %path`box
  142.     %_pause
  143.     capture noisily graph using %path`star
  144.     %_pause
  145.     capture noisily graph using %path`bar
  146.     %_pause
  147.     capture noisily graph using %path`pie
  148.     set more 1
  149.     _yesno "Would you like to see them again"
  150.     if lower("%answ")=="y" {
  151.         set more 0
  152.         capture noisily graph using %path`twoway
  153.         %_pause
  154.         capture noisily graph using %path`matrix
  155.         %_pause
  156.         capture noisily graph using %path`hist
  157.         %_pause
  158.         capture noisily graph using %path`oneway
  159.         %_pause
  160.         capture noisily graph using %path`box
  161.         %_pause
  162.         capture noisily graph using %path`star
  163.         %_pause
  164.         capture noisily graph using %path`bar
  165.         %_pause
  166.         capture noisily graph using %path`pie
  167.         set more 1
  168.     capture }
  169. capture }
  170.  
  171. capture program drop _yesno
  172. macro def answ
  173.  
  174. #delimit ;
  175.  
  176. di _n(9) in wh
  177. "Enough preliminary" _n
  178. "------------------" _n ;
  179.  
  180. di _n in wh _dup(79) "-" _n in gr
  181. "To teach you how to use Stata's graphics capabilities, we will use the auto-"
  182. _n
  183. "mobile data that we used in the introduction.  Remember that this data is" _n
  184. "stored in the file 'auto.dta':" _n
  185. in wh _dup(79) "-" _n(2)
  186. ". use %path`auto, clear" ;
  187. noisily use %path`auto, clear ;
  188.  
  189.  
  190. di _n(6) in wh _dup(79) "-" _n in gr
  191. "We will begin with the two-way scatterplot, plotting miles per gallon against"
  192. _n
  193. "engine displacement:" _n
  194. in wh _dup(79) "-" _n(2)
  195. ". graph mpg displ" ;
  196. set more 0 ; more ;
  197. capture noisily graph mpg displ ;
  198. set more 1 ;
  199.  
  200. di _n(12) in wh _dup(79) "-" _n in gr
  201. "Stata will allow us to manipulate the basic image and add labeling, grid" _n
  202. "lines, and titles.  Let's ask Stata to label the axes:" _n
  203. in wh _dup(79) "-" _n(2)
  204. ". graph mpg displ, xlabel ylabel" ;
  205. set more 0 ; more ;
  206. capture noisily graph mpg displ, xlabel ylabel ;
  207. set more 1 ;
  208.  
  209. di _n(7) in wh _dup(79) "-" _n in gr
  210. "We'll get to grid lines and titles in a moment, but first we'll show you"
  211. _n
  212. "how to vary the text size.  The Stata parameter "
  213. in wh "textsize" in gr " says how big the" _n
  214. "characters are supposed to be.  Its default value is 100, which means 100%" _n
  215. "of what Stata thinks looks good.  Let's redraw our last image varying the" _n
  216. in wh "textsize" in gr ":" _n
  217. in wh _dup(79) "-" _n(2)
  218. ". set textsize 200" _n(2)
  219. ". graph mpg displ, xlabel ylabel" ;
  220. set more 0 ; more ;
  221. set textsize 200 ;
  222. capture noisily graph mpg displ, xlabel ylabel gap(3);
  223. set more 1 ;
  224.  
  225. di in wh _n(3)
  226. ". set textsize 125" _n(2)
  227. ". graph mpg displ, xlabel ylabel" ;
  228. set more 0 ; more ;
  229. set textsize 125 ;
  230. capture noisily graph mpg displ, xlabel ylabel gap(4);
  231. set more 1 ;
  232.  
  233. di in wh _n(3)
  234. ". set textsize 75" _n(2)
  235. ". graph mpg displ, xlabel ylabel" ;
  236. set more 0 ; more ;
  237. set textsize 75 ;
  238. capture noisily graph mpg displ, xlabel ylabel gap(4);
  239. set more 1 ;
  240.  
  241. di _n(17) in wh _dup(79) "-" _n in gr
  242. "You get the idea.  We're not going to play with "
  243. in wh "textsize" in gr " again, but you may" _n
  244. "want to.  We recommend that PC users with the Color Graphics Adapter '"
  245. in wh "set" _n
  246. "textsize 125" in gr "'.  We'll set it back to the default 100:" _n
  247. in wh _dup(79) "-" _n(2)
  248. ". set textsize 100" ;
  249. set textsize 100 ;
  250. set more 0 ; more ; set more 1 ;
  251.  
  252.  
  253. di _n(7) in wh _dup(79) "-" _n in gr
  254. "Now let's return to adding titles and grid lines.  There are two titles on" _n
  255. "every side of the graph, called "
  256. in wh "t1title" in gr ", " in wh "t2title" in gr ", "
  257. in wh "b1title" in gr ", " in wh "b2title" in gr ", "
  258. in wh "l1title" in gr "," _n
  259. in wh "l2title" in gr ", "
  260. in wh "r1title" in gr ", and " in wh "r2title"
  261. in wh ".  The '" in wh "t" in gr "', '"
  262. in wh "b" in gr "', '" in wh "l" in gr "', and '" in wh "r"
  263. in gr "' stand for top," _n
  264. "bottom, left, and right.  The "
  265. in wh "b1title" in gr " is also known as simply the "
  266. in wh "title" in gr "." _n ;
  267.  
  268. di in gr
  269. "Here's a graph showing where all the titles are:" _n
  270. in wh _dup(79) "-" _n(2)
  271. ". graph mpg displ, t1title(This is the t1title)" _n
  272. "                   t2title(This is the t2title)" _n
  273. "                   b1title(This is the b1title also known as title)" _n
  274. "                   b2title(This is the b2title)" _n
  275. "                   r1title(This is the r1title)" _n
  276. "                   r2title(This is the r2title)" _n
  277. "                   l1title(This is the l1title)" _n
  278. "                   l2title(This is the l2title)" ;
  279. set more 0 ; more ;
  280. capture noisily gr mpg displ, gap(4)
  281.     t1(This is the t1title) t2(This is the t2title)
  282.     b1("    This is the b1title also known as title")
  283.     b2(This is the b2title)
  284.     r1(This is the r1title) r2(This is the r2title)
  285.     l1(This is the l1title) l2(This is the l2title) ;
  286. set more 1 ;
  287.  
  288. di _n(13) in wh _dup(79) "-" _n in gr
  289. "In practice, no one types out words like "
  290. in wh "l1title" in gr " and " in wh "b2title" in gr ".  The abbre-" _n
  291. "viations are "
  292. in wh "l1" in gr ", "
  293. in wh "l2" in gr ", "
  294. in wh "t1" in gr ", "
  295. in wh "t2" in gr ", "
  296. in wh "b1" in gr ", "
  297. in wh "b2" in gr ", "
  298. in wh "r1" in gr ", and "
  299. in wh "r2" in gr ".  The more easily remem-" _n
  300. "bered word " in wh "title"
  301. in gr " is a synonym for " in wh "b1title"
  302. in gr ", and it can be abbreviated as" _n
  303. in wh "ti" in gr "." _n ;
  304.  
  305. di in gr
  306. "So now, let's go back and title our graph:" _n
  307. in wh _dup(79) "-" _n(2)
  308. ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
  309. "                   t1(1978 Automobile Data)" ;
  310. set more 0 ; more ;
  311. capture noisily graph mpg displ, xlabel ylabel gap(4)
  312.     title("    MPG vs. Engine Displacement")
  313.     t1(1978 Automobile Data) ;
  314. set more 1 ;
  315.  
  316. di _n(2) in wh _dup(79) "-" _n in gr
  317. "You might prefer:" _n
  318. in wh _dup(79) "-" _n(2)
  319. ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
  320. "                   t1(1978 Automobile Data) border" ;
  321. set more 0 ; more ;
  322. capture noisily graph mpg displ, xlabel ylabel gap(4)
  323.     title("    MPG vs. Engine Displacement")
  324.     t1(1978 Automobile Data) border t2(" ") ;
  325. set more 1 ;
  326.  
  327. di _n(2) in wh _dup(79) "-" _n in gr
  328. "Or even:" _n
  329. in wh _dup(79) "-" _n(2)
  330. ". graph mpg displ, xlabel ylabel ti(MPG vs. Engine Displacement)" _n
  331. "                   t1(1978 Automobile Data) border tlabel rlabel" ;
  332. set more 0 ; more ;
  333. capture noisily graph mpg displ, xlabel ylabel gap(4)
  334.     title("  MPG vs. Engine Displacement")
  335.     t1(1978 Automobile Data) border t2(" ") tlab rlab;
  336. set more 1 ;
  337.  
  338.  
  339.  
  340.  
  341. di _n(2) in wh _dup(79) "-" _n in gr
  342. "We can also ask Stata to overlay a grid by suppressing the axes and drawing" _n
  343. "lines across the graph.  In the case of the y-axis, we tell Stata to label" _n
  344. "and line where it thinks best.  In the case of the x-axis, we tell Stata" _n
  345. "exactly where we want the lines and labels:" _n
  346. in wh _dup(79) "-" _n(2)
  347. ". graph mpg displ, ylabel yline t1(1978 Automobile Data)" _n
  348. "                   xlabel(75,175,275,375,475) xline(75,175,275,375,475)" _n
  349. "                   noaxis ti(MPG vs. Engine Displacement)";
  350. set more 0 ; more ;
  351. capture noisily graph mpg displ, xlabel(75,175,275,375,475) ylabel
  352.     xline(75,175,275,375,475) yline noaxis
  353.     ti("    MPG vs. Engine Displacement")
  354.     t1(1978 Automobile Data) gap(4) ;
  355. set more 1 ;
  356.  
  357. di _n(2) in wh _dup(79) "-" _n in gr
  358. "Perhaps we should label the right and top of the image, too:" _n
  359. in wh _dup(79) "-" _n(2)
  360. ". graph mpg displ, ylabel yline t1(1978 Automobile Data)" _n
  361. "                   xlabel(75,175,275,375,475) xline(75,175,275,375,475)" _n
  362. "                   tlabel(75,175,275,375,475) rlabel" _n
  363. "                   noaxis ti(MPG vs. Engine Displacement)";
  364. set more 0 ; more ;
  365. capture noisily graph mpg displ, xlabel(75,175,275,375,475) ylabel rlabel
  366.     xline(75,175,275,375,475) yline noaxis
  367.     tlabe(75,175,275,375,475)
  368.     ti("MPG vs. Engine Displacement")
  369.     t1(1978 Automobile Data) gap(4) ;
  370. set more 1 ;
  371.  
  372.  
  373. di _n(18) in wh _dup(79) "-" _n in gr
  374. "Let's go back to the simple image, but this time ask Stata to connect the" _n
  375. "points with straight lines.  Our first example will not be what we want:" _n
  376. in wh _dup(79) "-" _n(2)
  377. ". graph mpg displ, connect(l)" ;
  378. set more 0 ; more ;
  379. capture noisily graph mpg displ, c(l) gap(4)
  380.     t1("(This is a mess because Stata connected the points in")
  381.     t2("the order of the data!)") ;
  382. set more 1 ;
  383.  
  384. di _n(2) in wh _dup(79) "-" _n in gr
  385. "That didn't work out so well.  We can tell Stata to connect the points in" _n
  386. "the order of the x-axis by adding the '"
  387. in wh "sort" in gr "' option.  We will also begin" _n
  388. "abbreviating our commands and options because Stata users seldom type out" _n
  389. "the words:" _n
  390. in wh _dup(79) "-" _n(2)
  391. ". gr mpg displ, c(l) sort" ;
  392. set more 0 ; more ;
  393. capture noisily graph mpg displ, c(l) sort gap(4);
  394. set more 1 ;
  395.  
  396. di _n(6) in wh _dup(79) "-" _n in gr
  397. "That was better, but connecting the points was still not very informative." _n
  398. "Stata can connect points using a smooth cubic spline.  It does this by di-" _n
  399. "viding the x-axis into 200 intervals and calculating the median x and y" _n
  400. "value in each.  Those values are then used for the calculation and drawing" _n
  401. "of a cubic spline.  We get this by saying "
  402. in wh "connect(s)" in gr " rather than " in wh "connect(l)" in gr "." _n
  403. in wh _dup(79) "-" _n(2)
  404. ". gr mpg displ, c(s)" ;
  405. set more 0 ; more ;
  406. capture noisily graph mpg displ, c(s) gap(4)
  407.     t1("(Oops!  It's oscillating.  We can fix that.)") ;
  408. set more 1 ;
  409.  
  410. di _n(2) in wh _dup(79) "-" _n in gr
  411. "We can use splines to achieve data smoothing by reducing the number of bands"
  412. _n
  413. "Stata uses to fit the spline function.  By default, Stata uses 200 bands." _n
  414. "Let's try just 8." _n
  415. in wh _dup(79) "-" _n(2)
  416. ". gr mpg displ, c(s) bands(8)" ;
  417. set more 0 ; more ;
  418. capture noisily graph mpg displ, c(s) bands(8) gap(4);
  419. set more 1 ;
  420.  
  421. di _n(2) in wh _dup(79) "-" _n in gr
  422. "Splines are only one way of graphically summarizing data.  We could, for" _n
  423. "instance, draw the regression line.  To do this we must run the regression,"
  424. _n
  425. "retrieve the prediction, and then make the graph:" _n
  426. in wh _dup(79) "-" _n(2)
  427. ". regress mpg displ" ;
  428. capture noisily regress mpg displ ;
  429. set more 0 ; more ; set more 1 ;
  430. di _n(3) in wh ". predict hat" ;
  431. capture noisily predict hat ;
  432. di _n in wh ". gr mpg hat displ, symbol(Oi) c(.l) sort" ;
  433. set more 0 ; more ;
  434. capture noisily gr mpg hat displ, symbol(Oi) c(.l) sort ;
  435. set more 1 ;
  436.  
  437. di _n(8) in wh _dup(79) "-" _n in gr
  438. "We played some games in the last graph with the "
  439. in wh "symbol" in gr " and " in wh "connect" in gr " options." _n
  440. "You can learn how they work by typing '"
  441. in wh "help graph" in gr "' at the conclusion of this"
  442. _n
  443. "tutorial." _n(2)
  444. "Since we know what we're doing, we can even combine the data, the spline," _n
  445. "and the regression line in a single image:" _n
  446. in wh _dup(79) "-" _n(2)
  447. ". gr mpg hat displ, s(Oi) c(sl) sort bands(8)" ;
  448. set more 0 ; more ;
  449. capture noisily gr mpg hat displ, s(Oi) c(sl) sort bands(8) gap(4) ;
  450. set more 1 ;
  451.  
  452. di _n(2) in wh _dup(79) "-" _n in gr
  453. "You probably suspect that Stata has different point styles -- circles, tri-" _n
  454. "angles, squares, and the like -- and you're right.  More importantly, Stata" _n
  455. "also has the unique ability to use variables in your data as the plotting" _n
  456. "symbol.  For instance, in our data we have a variable called make recording" _n
  457. "the make of the car:" _n
  458. in wh _dup(79) "-" _n(2)
  459. ". list make in 1/5" ;
  460. noisily list make in 1/5 ;
  461.  
  462. di _n(2) in wh _dup(79) "-" _n in gr
  463. "We can use this variable as our plotting symbol:" _n
  464. in wh _dup(79) "-" _n(2)
  465. ". graph mpg displ, symbol([make])" ;
  466. set more 0 ; more ;
  467. capture noisily graph mpg displ, s([make]) gap(4)
  468.     t1("(You wouldn't want to publish this graph, but it")
  469.     t2("is useful for analysis.)") ;
  470. set more 1 ;
  471.  
  472. drop gratio length trunk hdroom ;
  473.  
  474. di _n(2) in wh _dup(79) "-" _n in gr
  475. "There was too much data to make a presentation quality graph.  You might" _n
  476. "also have noticed that the words were trimmed to 8 characters.  You can" _n
  477. "increase that, but it would only increase the amount of overprinting." _n(2)
  478. "Let's restrict ourselves to the foreign cars, increase the point size to" _n
  479. "125% of what Stata normally uses, and trim the words at 32 characters:" _n
  480. in wh _dup(79) "-" _n(2)
  481. ". graph mpg displ if foreign, s([make]) psize(125) trim(32)" ;
  482. set more 0 ; more ;
  483. capture noisily graph mpg displ if foreign, s([make]) ps(125) tr(32) gap(4)
  484.     xsca(.,170)
  485.     t1("(We still have overprinting)") ;
  486. set more 1 ;
  487.  
  488. di _n(7) in wh _dup(79) "-" _n in gr
  489. "If we were willing to go to some work, we could use this capability to draw"
  490. _n
  491. "a truly presentable graph.  Remember that we ran a regression of mpg on" _n
  492. "displ.  We could use Stata's standard circle symbol for most of the data" _n
  493. "and the text symbols for the outliers.  To do that, first we need the re-" _n
  494. "siduals from our previous regression:" _n
  495. in wh _dup(79) "-" _n(2)
  496. ". predict res, residuals" ;
  497. noisily predict res, residuals ;
  498.  
  499. di _n(2) in wh _dup(79) "-" _n in gr
  500. "Next, we need to examine those residuals to find out what an outlier is:" _n
  501. in wh _dup(79) "-" _n(2)
  502. ". summarize res, detail" ;
  503. set more 0 ; more ; set more 1 ;
  504. noisily summarize res, detail ;
  505.  
  506. di _n(2) in wh _dup(79) "-" _n in gr
  507. "We'll call an outlier anything below the 5th or above the 95th percentile." _n
  508. "We see that the corresponding numbers are -6.11 and 9.2:" _n
  509. in wh _dup(79) "-" _n(2)
  510. ". generate outlier = mpg if res<-6.11 | res>9.2" ;
  511. set more 0 ; more ; set more 1 ;
  512. noisily gen outlier = mpg if res<-6.11 | res>9.2 ;
  513.  
  514. di _n in wh ". gen goodpt = mpg if outlier==." ;
  515. noisily gen goodpt = mpg if outlier==. ;
  516.  
  517. replace outlier=. in 69 ;
  518. replace good=. in 57 ;
  519.  
  520. di _n in wh
  521. ". graph goodpt outlier hat displ, s(O[make]i) c(..l) sort" _n
  522. "        l1(Mileage) t1(Outliers named) psize(115) trim(32)" ;
  523. set more 0 ; more ;
  524. capture noisily graph goodpt outlier hat displ,
  525.     s(O[make]i) c(..l) sort
  526.     l1("Mileage") t1(Outliers named) psize(115) trim(32) gap(4) ;
  527. set more 1;
  528.  
  529. di _n(15) in wh _dup(79) "-" _n in gr
  530. "You probably think that, by now, you know everything there is to know about" _n
  531. "the two-way scatterplot.  You're wrong.  Stata has the ability to draw dif-" _n
  532. "ferent scatterplots in the same image for groups of the data.  Our data con-"
  533. _n
  534. "tains both foreign and domestic cars.  Let's draw separate graphs for foreign"
  535. _n
  536. "cars, domestic cars, and both groups combined:" _n
  537. in wh _dup(79) "-" _n(2)
  538. ". graph mpg displ, ylabel xlabel by(foreign) total" ;
  539. set more 0 ; more ;
  540. capture noisily graph mpg displ, ylabel xlabel by(foreign) total ;
  541. set more 1 ;
  542.  
  543. di _n(2) in wh _dup(79) "-" _n in gr
  544. "We'll come back to this feature later.  It is not limited to only two groups;"
  545. _n
  546. "in fact, Stata can put up to 400 graphs on a single screen!" _n
  547. in wh _dup(79) "-" _n(2) ;
  548.  
  549. use %path`auto, clear ;
  550. set more 0 ; more ; set more 1 ;
  551.  
  552. di _n(16) in wh _dup(79) "-" _n in gr
  553. "One of Stata's more powerful images is the scatterplot matrix, a generaliza-"
  554. _n
  555. "tion of the scatterplot.  Making them is easy:" _n
  556. in wh _dup(79) "-" _n(2)
  557. ". graph mpg displ weight gratio, matrix label" ;
  558. set more 0 ; more ;
  559. gr mpg displ weight gratio, mat label
  560. t1("On the first row are graphs of mileage against displ, weight,")
  561. t2("and gratio")
  562. l1("On the second row are graphs of displ")
  563. l2("against mileage, weight, and gratio")
  564. r2("On the third row are graphs of weight")
  565. r1("against mileage, displ, and gratio")
  566. b2("On the fourth row are graphs of gratio against mileage, displ, weight") ;
  567. set more 1 ;
  568.  
  569. di _n(2) in wh _dup(79) "-" _n in gr
  570. "Stata views scatterplot matrices as a generalization of the standard two-way"
  571. _n
  572. "scatterplot, so anything we can do to a two-way scatterplot we can do to a" _n
  573. "scatterplot matrix." _n(2)
  574. "In the next example, we redraw the scatterplot matrix and include a smoothed"
  575. _n
  576. "spline function using 8 bands:" _n
  577. in wh _dup(79) "-" _n(2)
  578. ". graph mpg displ weight gratio, matrix label c(s) bands(8)" ;
  579. set more 0 ; more ;
  580. capture noisily graph mpg displ weight gratio, matrix label c(s) bands(8) ;
  581. set more 1 ;
  582.  
  583. di _n(3) in wh _dup(79) "-" _n in gr
  584. "So far we have drawn 4-by-4 scatterplots.  You may wonder how far we can go."
  585. _n
  586. "Stata will draw 30-by-30 scatterplots, which are so small as to be unreadable."
  587. _n
  588. "Here is an 8-by-8 matrix:" _n
  589. in wh _dup(79) "-" _n(2)
  590. ". graph  price mpg weight displ hdroom trunk length turn, matrix label sy(.)" ;
  591. set more 0 ; more ;
  592. capture noisily gr price mpg weight displ hdroom trunk length turn,
  593.     matrix label sy(.);
  594. set more 1 ;
  595.  
  596.  
  597. di _n(18) in wh _dup(79) "-" _n in gr
  598. "Stata can draw histograms.  Below we draw a histogram using 7 bins with an" _n
  599. "overlaid normal:" _n
  600. in wh _dup(79) "-" _n(2)
  601. ". graph mpg, bin(7) ylabel xlabel normal" ;
  602. set more 0 ; more ;
  603. capture noisily graph mpg, bin(7) ylabel xlabel normal gap(4) ;
  604. set more 1 ;
  605.  
  606. di _n(2) in wh _dup(79) "-" _n in gr
  607. "We can combine the histogram style with the "
  608. in wh "by()" in gr " option to obtain separate" _n
  609. "histograms by car type:" _n
  610. in wh _dup(79) "-" _n(2)
  611. ". graph mpg, bin(7) ylabel xlabel normal by(foreign) total" ;
  612. set more 0 ; more ;
  613. capture noisily graph mpg, bin(7) ylabel xlabel normal by(foreign) total ;
  614. set more 1 ;
  615.  
  616.  
  617.  
  618. di _n(14) in wh _dup(79) "-" _n in gr
  619. "In addition to histograms, Stata provides the one-way scatterplot.  One-way" _n
  620. "scatterplots show the distribution of the data by drawing an axis and then" _n
  621. "placing vertical lines everywhere that data occurs." _n(2)
  622. "Here's an example of a one-way scatterplot of price:" _n
  623. in wh _dup(79) "-" _n(2)
  624. ". graph price, oneway by(foreign) total ti(Distribution of Price)" ;
  625. set more 0 ; more ;
  626. capture noisily graph price, oneway by(foreign) total
  627.     ti("Distribution of Price") ;
  628. set more 1 ;
  629.  
  630. di _n(2) in wh _dup(79) "-" _n in gr
  631. "A box plot shows the distribution of the data by drawing a box showing the" _n
  632. "median, 25th and 75th percentiles, and the upper- and lower-adjacent values."
  633. _n
  634. "Points outside the adjacent values, called outside values, are plotted indi-"
  635. _n
  636. "vidually." _n
  637. in wh _dup(79) "-" _n(2)
  638. ". graph price, box by(foreign) total ylabel" ;
  639. set more 0 ; more ;
  640. capture noisily graph price, box by(foreign) total ylabel
  641.     ti("Comparison of Domestic and Foreign Cars") ;
  642. set more 1 ;
  643.  
  644. di _n(3) in wh _dup(79) "-" _n in gr
  645. "Stata can combine the box and one-way methods of showing a distribution," _n
  646. "resulting in a more informative display:" _n
  647. in wh _dup(79) "-" _n(2)
  648. ". graph price, oneway box by(foreign) total" ;
  649. set more 0 ; more ;
  650. capture noisily graph price, oneway box by(foreign) total
  651.     ti("Comparison of Domestic and Foreign Cars") ;
  652. set more 1 ;
  653.  
  654.  
  655. di _n(2) in wh _dup(79) "-" _n in gr
  656. "Stata even allows you to combine two-way scatterplots, one-way scatterplots,"
  657. _n
  658. "and box plots into a single, informative diagram:" _n
  659. in wh _dup(79) "-" _n(2)
  660. ". graph price weight, twoway oneway box" ;
  661. set more 0 ; more ;
  662. capture noisily graph price weight, twoway oneway box gap(6)
  663.     t1("(We call this our kitchen-sink diagram.)")
  664.     t2(" ") ;
  665. set more 1 ;
  666. di in wh _n
  667. ". graph price weight, twoway oneway box by(foreign) total" ;
  668. set more 0 ; more ;
  669. capture noisily graph price weight, twoway oneway box by(foreign) total ;
  670. set more 1 ;
  671.  
  672. replace make="Renault" in 65 ;
  673.  
  674. di _n(20) in wh _dup(79) "-" _n in gr
  675. "Stata can draw star charts:" _n
  676. in wh _dup(79) "-" _n(2)
  677. ". graph price-displ if foreign, star label(make)" ;
  678. set more 0 ; more ;
  679. capture noisily graph price-displ if foreign, star label(make) bsize(130) ;
  680. set more 1 ;
  681.  
  682. drop _all ;
  683. label drop _all ;
  684. input str4 year labor parts advert overhead profits dlabor ilabor ;
  685. FY82    1000     500   100      20    -900     800     200 ;
  686. FY83    2000     800   200      40    -400    1600     400 ;
  687. FY84    2200     900   300      50    -200    1760     440 ;
  688. FY85    2400    1000   400     100      40    1920     480 ;
  689. FY86    2400    1000   400      60     100    1920     480 ;
  690. FY87    2800    1200   500     110     210    2240     560 ;
  691. FY88    2900    1500   550     120     450    2320     580 ;
  692. end ;
  693. label var year     "Fiscal Year" ;
  694. label var labor    "Labor costs" ;
  695. label var parts    "Raw materials" ;
  696. label var advert   "Advertising" ;
  697. label var overhead "Overhead" ;
  698. label var profits  "Profits" ;
  699. label var dlabor   "Direct labor" ;
  700. label var ilabor   "Indirect labor" ;
  701. format year %4s ;
  702. format labor parts advert dlabor ilabor %6.0g ;
  703. format overhead %8.0g ;
  704. format profits %7.0g ;
  705.  
  706. di _n(8) in wh _dup(79) "-" _n in gr
  707. "To demonstrate Stata's bar and pie chart capabilities, we will use data on" _n
  708. "costs and revenues for a hypothetical firm.  We have data for FY82 to FY88."
  709. _n
  710. "We've already loaded the data:" _n
  711. in wh _dup(79) "-" _n(2)
  712. ". list" ;
  713. noisily list, nodisplay ;
  714.  
  715. di _n(2) in wh _dup(79) "-" _n in gr
  716. "Let's begin by drawing a bar chart:" _n
  717. in wh _dup(79) "-" _n(2)
  718. ". graph labor parts overhead profits, bar by(year) ylabel yline noaxis" ;
  719. set more 0 ; more ;
  720. capture noisily graph labor parts overhead profits, bar by(year) ylabel yline
  721.     noaxis     pen(2354) ;
  722. set more 1 ;
  723.  
  724.  
  725. di _n(13) in wh _dup(79) "-" _n in gr
  726. "Perhaps you prefer stacked bar charts:" _n
  727. in wh _dup(79) "-" _n(2)
  728. ". graph labor parts overhead profits, bar by(year) ylabel yline noaxis stack" ;
  729. set more 0 ; more ;
  730. capture noisily graph labor parts overhead profits, bar by(year) ylabel yline
  731.     noaxis     pen(2354) stack ;
  732. set more 1 ;
  733.  
  734. di _n(13) in wh _dup(79) "-" _n in gr
  735. "Pie charts are equally easy:" _n
  736. in wh _dup(79) "-" _n(2)
  737. ". graph dlabor ilabor parts advert overhead, pie by(year)" ;
  738. set more 0 ; more ;
  739. capture noisily graph dlabor ilabor parts advert overhead, pie by(year) ;
  740. set more 1 ;
  741.  
  742. di _n(18) in wh _dup(79) "-" _n in gr
  743. "Stata has one more capability:  the ability to combine graphs.  At the be-" _n
  744. "ginning of this tutorial, we showed you examples of each of Stata's styles." _n
  745. "Each of those examples is saved in a disk file and was created using "
  746. in wh "graph" in gr "'s"_n
  747. in wh "saving" in gr " option." _n(2)
  748. "The files were named twoway.gph, matrix.gph, hist.gph, and so on.  We can" _n
  749. "tell Stata to combine the graphs into a single image with the '"
  750. in wh "graph using" in gr "'" _n
  751. "command:" _n
  752. in wh _dup(79) "-" _n ;
  753.  
  754. di in wh
  755. ". graph using %path`twoway  %path`matrix" _n
  756. "              %path`hist    %path`oneway,  margin(10)" _n
  757. "              title(Summary of Temperature Data)" ;
  758. set more 0 ; more ;
  759. capture noisily graph using %path`twoway %path`matrix
  760.     %path`hist %path`oneway, mar(10)
  761.     title(Summary of Temperature Data) ;
  762. set more 1 ;
  763.  
  764. di _n(18) in wh
  765. "Find out more" _n
  766. "-------------" _n ;
  767.  
  768. di in gr
  769. "You can learn more about the "
  770. in wh "graph" in gr " command by typing '"
  771. in wh "help graph" in gr "'." _n(2)
  772. "If you are interested in reading more about the kinds of images presented" _n
  773. "here, we highly recommend two books:" _n ;
  774.  
  775. di in wh _col(10)
  776. "Graphical Methods for Data Analysis" in gr ", by John Chambers," _n
  777. _col(10) "William Kleiner, and Paul A. Tukey, The Wadsworth" _n
  778. _col(10) "Statistics/Probability Series" _n ;
  779.  
  780. di in wh _col(10)
  781. "The Visual Display of Quantitative Information" in gr ", by Edward" _n
  782. _col(10) "R. Tufte, Graphics Press, Chesire, Connecticut" _n(10) ;
  783. set more 0 ; more ; set more 1 ;
  784.  
  785.  
  786. di _n(4) in white
  787. "Demonstration ends" _n
  788. "------------------" _n ;
  789.  
  790. macro define F6 "do %path`tables.tut;" ;
  791.  
  792. di in green
  793. "That concludes our short demonstration, but there's much more.  We now return"
  794. _n
  795. "control to you.  Some suggestions:" _n ;
  796.  
  797. di in green
  798. "If you ..." _col(34) "Then we will show you ..." _n
  799. "    Press " in white "F5" in green _col(38) "a table of tutorial contents" _n
  800. "    Press " in white "F6" in green _col(38) "the next tutorial, "
  801. in white "tables.tut" _n ;
  802.  
  803. run %path`tobuy.tut ;
  804.